package com.kindness.frate.activity;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.kindness.frate.R;
import com.kindness.frate.bean.TimeBean;
import com.kindness.frate.fragment.MainFragment;
import com.kindness.frate.fragment.UserFragment;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.tablayout.CommonTabLayout;
import com.kindnesslibrary.tablayout.CustomTabEntity;
import com.kindnesslibrary.tablayout.OnTabSelectListener;
import com.kindnesslibrary.tablayout.TabEntity;
import com.kindnesslibrary.timepicker.OptionsPickerView;
import com.kindnesslibrary.timepicker.TimePickerView;
import com.kindnesslibrary.util.ToastUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends BaseActivity {

    CommonTabLayout ctl_main;
    private String[] mTitles = {"首页", "消息", "联系人", "更多"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select, R.mipmap.tab_more_select};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private Fragment currentFragment;

    TimePickerView pvTime;
    OptionsPickerView pvOptions;
    private ArrayList<TimeBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<TimeBean>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<TimeBean>>> options3Items = new ArrayList<>();

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ctl_main = getView(R.id.ctl_main);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        ctl_main.setTabData(mTabEntities);
        ctl_main.showMsg(1, 100);
        ctl_main.setMsgMargin(1, -10, 5);
        mFragments.add(MainFragment.newInstance());
        mFragments.add(MainFragment.newInstance());
        mFragments.add(UserFragment.newInstance());
        mFragments.add(UserFragment.newInstance());
        switchFragment(mFragments.get(0));

        pvTime = new TimePickerView(this, TimePickerView.Type.MONTH_DAY_HOUR_MIN);
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        options1Items.add(new TimeBean("现在"));
        options1Items.add(new TimeBean("今天"));
        options1Items.add(new TimeBean("明天"));
        options1Items.add(new TimeBean("后天"));

        ArrayList<TimeBean> options5Items = new ArrayList<>();
        options5Items.add(new TimeBean("好的"));
        options5Items.add(new TimeBean("时机"));
        options5Items.add(new TimeBean("艾克"));
        options5Items.add(new TimeBean("积分"));
        ArrayList<TimeBean> options6Items = new ArrayList<>();
        options6Items.add(new TimeBean("酷狗"));
        options6Items.add(new TimeBean("苦瓜"));
        ArrayList<TimeBean> options7Items = new ArrayList<>();
        options7Items.add(new TimeBean("觉得"));
        options7Items.add(new TimeBean("肯定"));
        options7Items.add(new TimeBean("开始"));
        options7Items.add(new TimeBean("公开"));
        ArrayList<TimeBean> options8Items = new ArrayList<>();
        options8Items.add(new TimeBean("离开"));
        options8Items.add(new TimeBean("废了"));
        options2Items.add(options5Items);
        options2Items.add(options6Items);
        options2Items.add(options7Items);
        options2Items.add(options8Items);

        ArrayList<TimeBean> options10Items = new ArrayList<>();
        options10Items.add(new TimeBean("好的"));
        options10Items.add(new TimeBean("时机"));
        options10Items.add(new TimeBean("艾克"));
        options10Items.add(new TimeBean("积分"));
        ArrayList<TimeBean> options11Items = new ArrayList<>();
        options11Items.add(new TimeBean("酷狗"));
        options11Items.add(new TimeBean("苦瓜"));
        ArrayList<TimeBean> options4Items = new ArrayList<>();
        options4Items.add(new TimeBean("觉得"));
        options4Items.add(new TimeBean("肯定"));
        options4Items.add(new TimeBean("开始"));
        options4Items.add(new TimeBean("公开"));
        ArrayList<TimeBean> options9Items = new ArrayList<>();
        options9Items.add(new TimeBean("离开"));
        options9Items.add(new TimeBean("废了"));
        ArrayList<ArrayList<TimeBean>> a = new ArrayList<>();
        a.add(options10Items);
        a.add(options11Items);
        a.add(options4Items);
        a.add(options9Items);
        options3Items.add(a);
        options3Items.add(a);
        options3Items.add(a);
        options3Items.add(a);

        pvOptions = new OptionsPickerView(this);
        pvOptions.setPicker(options1Items, options2Items, options3Items, true);
        pvOptions.setTitle("月份");
        pvOptions.setCyclic(false, false, false);

    }

    /**
     * @param targetFragment
     */
    private void switchFragment(Fragment targetFragment) {
        if (targetFragment == null) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        //第一次使用switchFragment()时currentFragment为null，所以要判断一下
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        //如已添加过，则show就行了
        if (targetFragment.isAdded()) {
            transaction.show(targetFragment);
        } else {
            transaction.add(R.id.content_frame, targetFragment);
        }
        transaction.commit();
        currentFragment = targetFragment;
    }

    @Override
    protected void initEvent() {
        ctl_main.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchFragment(mFragments.get(position));
//                pvOptions.show();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        pvTime.setOnTimeSelectListener(date -> {
            ToastUtil.showToast(date.getTime() + "");
        });
        pvOptions.setOnoptionsSelectListener((options1, option2, options3) -> {
            ToastUtil.showToast("options1:" + options1);
        });
    }

}